<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

/**
 * 用户
 * Class User
 *
 * @package App\Models
 * @property int $id
 * @property string $username
 * @property string $email
 * @property string $password
 * @property string $confirmation_code
 * @property int $confirmed
 * @property string $remember_token
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 * @property int $uc_status
 * @property string $type
 * @property string $sms_confirmation_code
 * @property int $sms_confirmation_code_send
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\VerifierWorksAssign[] $assignedVerifyWorks
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUsername($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereEmail($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User wherePassword($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereConfirmationCode($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereConfirmed($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereRememberToken($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUcStatus($value)
 * @mixin \Eloquent
 */
class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
{
    use SoftDeletes, Authenticatable, Authorizable, CanResetPassword;

    /**
     * UC同步状态
     */
    const UC_STATUS_UNIMPORTED = 0;
    const UC_STATUS_IMPORTED = 1;
    const UC_STATUS_SYNCED = 2;
    
    const TYPE_STUDENT = 'student';
    const TYPE_TEACHER = 'teacher';
    const TYPE_COMPANY = 'company';
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');

    protected $dates = ['deleted_at', 'sms_confirmation_code_send'];

    protected $fillable = ['username', 'email', 'password', 'confirmed', 'uc_status' , 'mobile', 'type', 'sms_confirmation_code'];

    /**
     * 生成邮件激活码
     */
    public function generateConfirmationCode()
    {
        return md5(uniqid(mt_rand(), true));
    }
    
    /**
     * 生成短信激活码
     */
    public function generateSMSConfirmationCode( $length = 6 )
    {
        $this->sms_confirmation_code = mt_rand(pow(10,($length-1)), pow(10,$length)-1);
        $this->sms_confirmation_code_send = time();
        return $this->sms_confirmation_code;
    }
    
    public function checkSMSConfirmationCode( $code ){
        if ($code == $this->sms_confirmation_code && $this->sms_confirmation_code_send + 600 < time() ){
            return true;
        }else{
            return false;
        }
    }

    public function personalInfo(){
        return $this->morphTo('personalInfo' , 'type' , 'id');
    }

    public function honors()
    {
        return $this->belongsToMany('\App\Models\Honor' , 'honor_rate_teacher' , 'user_id' , 'honor_id');
    }
}
